<?php


namespace app\admin\controller;

use think\Controller;
use think\Request;

class Xtgl extends Controller
{
    function __construct(Request $request = null)
    {
        if( !session('admin') || session('admin')!='123'){
            echo "<script>
                        alert('对不起您并未登录用户！');
                        setTimeout(function(){
                            location.href='/index.php/admin/login';
                        },500)
                        </script>";
            exit();
        }else{
            $this->log=Controller('Log');
        }
        parent::__construct($request);
    }

    //修改角色
    public function cz_upjs()
    {

        $id=$_POST['id'];
        $data['name']=$_POST['name'];
        if(empty($_POST['qx_id'])){
            $data['cd_id']=[];
        }else{
            $data['cd_id']=json_encode($_POST['qx_id']);
        }
        $data['str']=$_POST['str'];
        $data['e_time'] = time();

        $new=db('juese')->where('id',$id)->update($data);

        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('修改角色 数据 -'.$data['name'].'- 的信息');
            echo 1;
        }
    }

    //添加角色操作
    public function cz_tjjs(){

        $data['name']=$_POST['name'];
        $isname=db('juese')->where('name',$data['name'])->find();
        if(!empty($isname)) {
            $this->error('重复的用户名称！');
        }
        $data['str']=$_POST['str'];
        $data['k_time']=time();
        $new=db('juese')->insert($data);
        if(empty($new)){
            $this->error('操作失败！网络错误');
        }else{
            $this->log->log_xie('添加角色 数据 -'.$data['name'].'- 的信息');
            $this->success('添加角色成功！', 'jsgl');
        }
    }
    //添加角色视图
    public function tjjs(){

        $new=db('caidan')->where('str',1)->select();
        $this->assign('qx',json_encode($new));
        return $this->fetch();
    }

    //角色管理视图
    public function jsgl()
    {

        $q=db('juese')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $new=db('juese')->order('k_time desc')->limit(10)->select();//ks数据10条
//        dump(json_decode($new[1]['cd_id']));
//        exit();

        for($x=0;$x<$q;$x++){
//            $qx[$x]=db('juese_qx')->where('js_id',$new[$x]['Id'])->select();
            $did=$new[$x]['cd_id'];
            if($did==null){
                //如果本次循环的cd_id为空就跳过这次循环
                continue;
            }
//            if($x=$q-1 && $did==null){
//                //如果最后一次的数据也都是空的话，就给一个默认空值！！
//                $qx=[];
//                break;
//            }
            $dc=json_decode($did);

            for($n=0;$n<count($dc);$n++){

                $cdid=$dc[$n];
                $cd[$n]=db('caidan')->where('Id',$cdid)->find();
            }

            $qx[$x]=$cd;
            $cd=[];
        }


//        dump($qx);
//        exit();

        //选中菜单项
        $this->assign('zqx',json_encode($qx));
        //s所有菜单选择项
        $qqx=db('caidan')->where('str',1)->select();
        $this->assign('qx',json_encode($qqx));
        $this->assign('q_y',json_encode($q_y));
        $this->assign('yh',json_encode($new));
        $this->assign('g',json_encode($q));
        return $this->fetch();
    }


    //操作添加用户
    public function cz_tjuser(){
        $data['name']=$_POST['name'];
        $isname=db('user')->where('name',$data['name'])->find();
        if(!empty($isname)) {
            $this->error('重复的用户名称！');
        }
        $data['pass']=$_POST['pass'];
        $data['js_id']=$_POST['js_id'];
        $data['z_name']=$_POST['z_name'];
        $data['agx']=$_POST['agx'];
        $data['phone']=$_POST['phone'];
        $data['str']=$_POST['pid'];
        $data['k_time']=time();
        $new=db('user')->insert($data);
        if(empty($new)){
            $this->error('操作失败！网络错误');
        }else{
            $this->log->log_xie('添加用户 数据 -'.$data['name'].'- 的信息');
            $this->success('添加用户成功！', 'yhgl');
        }

    }
    //添加用户视图
    public function tjuser(){
        $new=db('juese')->where('str',1)->select();//ks数据10条
        $this->assign('juese',json_encode($new));
        return $this->fetch();
    }
    //用户管理视图
    public function yhgl()
    {

        $q=db('user')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
//        $new=db('user')->order('k_time desc')->limit(10)->select();//ks数据10条
        $sql="
            select
            u.*,
            j.name jname,
            j.Id jid
            from
            user u LEFT JOIN juese j ON u.js_id=j.Id
            order by u.k_time desc limit 10
        ";
        $new=db()->query($sql);
        $this->assign('q_y',json_encode($q_y));
        $this->assign('yh',json_encode($new));
        $this->assign('g',json_encode($q));
        return $this->fetch();
    }
    //修改user用户操作
    public function cz_upuser(){
        $id=$_POST['id'];
        $data['name']=$_POST['name'];
        $data['z_name']=$_POST['z_name'];
        $data['agx']=$_POST['agx'];
        $data['phone']=$_POST['phone'];
        $data['js_id']=$_POST['js_id'];
        $data['str']=$_POST['str'];
        $data['e_time'] = time();
        $new=db('user')->where('id',$id)->update($data);
        if(empty($new)){
            $this->error('修改用户失败!网络错误');
        }else{
            $this->log->log_xie('修改用户 数据 -'.$data['name'].'- 的信息');
            $this->success('用户修改成功！', 'yhgl');
        }
    }
    //cz用户下一页
    public function userpage()
    {
        //当前页面
        $y = $_POST['y'];
        //要开始获取的位置
        $w = $y * 10 - 10;
        $d = $_POST['d'];
        //d的索引数据库
//        $new=db($d)->order('k_time desc')->limit($w,10)->select();
        $sql = '
             select
            u.*,
            j.name jname,
            j.Id jid
            from
            $d u LEFT JOIN juese j ON u.js_id=j.Id
            order by u.k_time desc limit $w,10
        ';
        $new = m()->query($sql);
        if (empty($new)) {
            echo -1;
        } else {
            echo json_encode($new);
        }
    }
        //科室管理
    public function ksgl()
    {
        $q=db('ks')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $new=db('ks')->order('k_time desc')->limit(10)->select();//ks数据10条
        $this->assign('q_y',json_encode($q_y));
        $this->assign('ks',json_encode($new));
        $this->assign('g',json_encode($q));
        return $this->fetch();
    }
    //2019-9-9升级 新的科室管理
    public function xksgl()
    {
        $q=db('ks')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
//        $new=db('ks')->order('k_time desc')->limit(10)->select();//ks数据10条
//        $zd=db('zhongduan')->where('ks_id',ks['Id'])->find();
//        $this->assign('zd',json_encode($zd));
        $sql="
            SELECT
            ks.*,
            zd.id zd_id,
            zd.ip zd_ip,
            zd.str zd_str
            FROM
            ks LEFT JOIN zhongduan zd ON ks.Id=zd.ks_id
            ORDER BY ks.k_time DESC limit 10;   
        ";
        $new=db()->query($sql);
        $this->assign('q_y',json_encode($q_y));
        $this->assign('ks',json_encode($new));
        $this->assign('g',json_encode($q));
        return $this->fetch();
    }
    //添加科室视图
    public function tjks()
    {
        return $this->fetch();
    }

    //删除科室 单一或者批量
    public function scks()
    {
        $id=$_POST['id'];
        $d=$_POST['d'];//是否批量操作
//        $db=$_POST['db'];//数据库名称
        if($d=='pl'){
            $zdid=$_POST['zd_id'];
            $new=db('ks')->delete($id);
            $xnew=db('zhongduan')->delete($zdid);
        }else{
            $new=db('ks')->where('id',$id)->delete();
            $xnew=db('zhongduan')->where('ks_id',$id)->delete();
        }
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('删除科室 数据 -'.json_encode($id).'- 的信息');
            echo 1;
        }
    }
    //修改科室
    public function xgks()
    {
            $id=$_POST['id'];
            $data['name']=$_POST['name'];
            $data['lx_id']=$_POST['lx_id'];
            $data['str']=$_POST['str'];
                $data['e_time'] = time();
            $new=db('ks')->where('id',$id)->update($data);
            if(empty($new)){
                echo -1;
            }else{
                $this->log->log_xie('修改科室 数据 -'.$id.'- 的信息');
                echo 1;
            }
    }
    //2019-9-12新的修改科室
    public function xxgks()
    {
        $id=$_POST['id'];
        $data['name']=$_POST['name'];
        $data['lx_id']=$_POST['lx_id'];
        $data['str']=$_POST['str'];
        $zd_id=$_POST['zd_id'];

        $xdata['ip']=$_POST['zd_ip'];
        $xdata['e_time']=time();

        $data['e_time'] = time();
        $new=db('ks')->where('id',$id)->update($data);
//        var_dump($zd_id,$xdata['ip']);
        $xnew=db('zhongduan')->where('id',$zd_id)->update($xdata);


       if(empty($new) || empty($xnew)){
            echo -1;
        }else{
            $this->log->log_xie('修改科室 数据 -'.$id.'- 的信息');
            echo 1;
        }
    }
    //添加科室操作
    public function cz_tjks()
    {
        $data['name']=$_POST['name'];
        if($data['name']==''){
            $this->error('操作失败！网络错误');
        }
        $isname=db('ks')->where('name',$data['name'])->find();
        if(!empty($isname)) {
              $this->error('重复的科室名称！');
        }
        $data['lx_id']=$_POST['lx_id'];
        $data['str']=$_POST['pid'];
        $data['k_time']=time();

        $new=db('ks')->insert($data);

        if(empty($new)){
            $this->error('操作失败！网络错误');
        }

        $ksid=db('ks')->order('id desc')->find();
//        var_dump($ksid);
        $xdata['ip']=$_POST['zd_ip'];
        $xdata['ks_id']=$ksid['Id'];
        $xdata['k_time']=time();


        $xnew=db('zhongduan')->insert($xdata);

        if(empty($new) || empty($xnew)){
             $this->error('操作失败！网络错误');  
        }else{
            $this->log->log_xie('添加科室 数据 -'.$data['name'].'- 的信息');
            $this->success('科室添加成功！', 'xksgl');
        }
    }
     //上一页下一页尾页
     public function page(){
        //当前页面
        $y=$_POST['y'];
        //要开始获取的位置
        $w=$y*10-10;
        $d=$_POST['d'];
        //d的索引数据库
         $new=db($d)->order('k_time desc')->limit($w,10)->select();

//        if($d=='yhhd_sp'){
//            $sql='
//            select
//            tg.*,
//            u.user_name as username,
//            y.bt as hdbt
//            from
//            ecs_yhhd_tg tg LEFT JOIN ecs_users u ON u.user_id=tg.userid
//            LEFT JOIN ecs_yhhd y ON y.id=tg.yhhdid
//            WHERE tg.shenpi !=0
//            order by tg.id desc limit $w-1,10
//        ';
//            $new=m()->query($sql);
//        }
        if(empty($new)){
            echo -1 ;
        }else{
            echo json_encode($new) ;
        }

    }
    //编辑修改菜单
    public function xgcd()
    {
        $id=$_POST['id'];
        $data['name']=$_POST['name'];
//        $data['lx_id']=$_POST['lx_id'];
        $data['str']=$_POST['str'];
        $data['e_time'] = time();
        $new=db('caidan')->where('id',$id)->update($data);
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('修改菜单 数据 -'.$data['name'].'- 的信息');
            echo 1;
        }
    }
    //删除菜单 单一或者批量
    public function sccd()
    {
        $id=$_POST['id'];
        $d=$_POST['d'];//是否批量操作
//        $db=$_POST['db'];//数据库名称
        if($d=='pl'){
            $new=db('caidan')->delete($id);
        }else{
            $new=db('caidan')->where('id',$id)->delete();
        }
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('删除菜单 数据 -'.json_encode($id).'- 的信息');
            echo 1;
        }
    }
    //添加菜单视图
    public function tjcd(){
        return $this->fetch();
    }
    //菜单管理视图
    public function cdgl(){
        $new=db('caidan')->select();
        $q=db('caidan')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $this->assign('q_y',json_encode($q_y));
        $this->assign('g',json_encode($q));
        $this->assign('cd',json_encode($new));
        return $this->fetch();
    }
//添加菜单操作
    public function cz_tjcd()
    {
        $data['name']=$_POST['name'];
        if($data['name']==''){
            $this->error('操作失败！网络错误');
        }
        $isname=db('caidan')->where('name',$data['name'])->find();
        if(!empty($isname)) {
            $this->error('重复的菜单名称！');
        }
//        $data['lx_id']=$_POST['lx_id'];
        $data['str']=$_POST['pid'];
        $data['k_time']=time();
        $new=db('caidan')->insert($data);
        if(empty($new)){
            $this->error('操作失败！网络错误');
        }else{
            $this->log->log_xie('添加菜单 数据 -'.$data['name'].'- 的信息');
            $this->success('菜单添加成功！', 'ksgl');
        }
    }
    //log日志视图
    public function log(){
        $q=db('log')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }

        $sql="
            SELECT
            l.*,
            u.name
            FROM
            log l LEFT JOIN user u ON l.user_id=u.Id
            WHERE l.str=1 ORDER BY l.k_time DESC limit 10 
        ";
        $new=db()->query($sql);

        $this->assign('q_y',json_encode($q_y));
        $this->assign('g',json_encode($q));
        $this->assign('log',json_encode($new));
        return $this->fetch();
    }
    //log日志上一页下一页尾页
    public function logpage(){
        //当前页面
        $y=$_POST['y'];
        //要开始获取的位置
        $w=$y*10-10;
        $d=$_POST['d'];
        //d的索引数据库
//        $new=db($d)->order('k_time desc')->limit($w,10)->select();

        $sql="
            SELECT
            l.*,
            u.name
            FROM
            log l LEFT JOIN user u ON l.user_id=u.Id
            WHERE l.str=1 ORDER BY l.k_time DESC limit $w,10 
        ";
        $new=db()->query($sql);
        if(empty($new)){
            echo -1 ;
        }else{
            echo json_encode($new) ;
        }

    }


  /*base64图片接收存储
         * @imgges 图片
         */
        public function base64add($image){
            //匹配出图片的格式
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$image, $result)) {
                $type = $result[2];  //图片格式
                $new_file = "upfiles/yhimg/" . date('Ymd', time()) . "/"; //存放的文件夹
                if (!file_exists($new_file)) {
                    //检查是否有该文件夹，如果没有就创建，并给予最高权限
                    mkdir($new_file, 0700);
                }
                $order_num = $this->OrderNum();
                $new_file = $new_file . $order_num . ".{$type}";
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $image)))) {
                    //成功返回保存图片的路径
                    return $new_file;
                } else {
                    //失败返回-1
                    return -1;
                }
            }
    }
    //图片名字生成
    function OrderNum()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }

}